// pages/addGoods/addGoods.js
let myRequest = require('../../config/myRequest.js');
let api = require('../../config/api.js');
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        domain: api.domain,
        page: {
            pageIndex: 1,
            pageSize: 10,
        },
        goodsList: [],
        noData: false,
        isEnd: false,
        keyword: '',
        multiIndex: [0, 0],
        multiArray: [],
        //一级
        xiaoquArr: [],
        xiaoquList: [],
        //二级
        classArr: [],
        classList: [],
        first: true,
        goodscat_id1: '',
        goodscat_id2: '',
        shows: false,
        goods_id: ''
    },
    keyWord(e) {
        const that = this;
        that.setData({
            keyword: e.detail.value
        })
    },
    addCart(e) {
        const that = this;
        wx.showModal({
            content: '是否导入商品',
            showCancel: true,
            confirmText: "确定",
            confirmColor: "#eb3223",
            success(res) {
                if (res.confirm) {
                    that.setData({
                        hiddenLoading: false,
                        goods_id: e.currentTarget.dataset.id,
                    })
                    myRequest.EasyHttp.post(api.reqUrls.getimport, {
                        token: app.globalData.token,
                        goods_id: e.currentTarget.dataset.id,

                    }).then(res => {
                        that.setData({
                            hiddenLoading: true
                        })
                        if (res.data.status == 1) {
                            wx.showToast({
                                title: res.data.msg,
                                icon: 'none',
                            })
                            var pages = getCurrentPages(); //当前页面栈
                            if (pages.length > 1) {
                                var beforePage = pages[pages.length - 2]; //获取上一个页面实例对象
                                beforePage.changeData(); //触发父页面中的方法
                            }
                            wx.navigateBack({
                                delta: 0,
                            })
                        } else if (res.data.status == -1) {
                            that.setData({
                                shows: true,
                            })
                        } else {
                            wx.showToast({
                                title: res.data.msg,
                                icon: 'none',
                            })
                        }
                    })
                }
            },
        })
    },
    /**
     * 搜索
     */
    searchSubmit() {
        const that = this;
        that.setData({
            [`page.pageIndex`]: 1
        })
        that.getData()
    },

    getData() {
        const that = this;
        let json = {
            token: app.globalData.token,
            page: that.data.page.pageIndex,
            rows: that.data.page.pageSize,
            goods_name: that.data.keyword
        }
        wx.showLoading({
            title: '加载中...',
        })
        myRequest.EasyHttp.post(api.reqUrls.getimportGoodsList, json).then(res => {
            wx.hideLoading();
            wx.stopPullDownRefresh();
            wx.hideNavigationBarLoading();
            if (res.data.status == 1) {
                let noData = true;
                if (res.data.result.length == 0) {
                    noData = false
                } else {
                    noData = true
                }
                console.log(res.data.result, 55555)
                that.setData({
                    goodsList: res.data.result,
                    isEnd: false,
                    noData
                })
            } else {
                wx.showModal({
                    showCancel: false,
                    content: res.data.msg,
                    success(res) {
                        if (res.confirm) {

                        }
                    }
                })
            }
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        const that = this;
        that.getData()
        this.showOne();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        const that = this;
        // that.setData({
        //     [`page.pageIndex`]: 1
        // })
        // that.getData()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        const that = this;
        that.setData({
            [`page.pageIndex`]: 1
        })
        wx.showNavigationBarLoading()
        that.getData()
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        const that = this;
        if (!that.data.isEnd) {
            wx.showLoading({
                title: '加载中...',
            })
        }
        let page = that.data.page.pageIndex;
        page++;
        let json = {
            token: app.globalData.token,
            page: page,
            rows: that.data.page.pageSize,
            goods_name: that.data.keyword
        }
        myRequest.EasyHttp.post(api.reqUrls.getimportGoodsList, json).then(res => {
            wx.hideLoading();
            wx.stopPullDownRefresh();
            wx.hideNavigationBarLoading();
            if (res.data.result.length == 0) {
                that.setData({
                    isEnd: true
                })
            } else {
                const goodsList = that.data.goodsList;
                let pageIndex = that.data.page.pageIndex * that.data.page.pageSize;
                res.data.result.forEach(res => {
                    // res.index = pageIndex + index + 1;
                    goodsList.push(res)
                })
                that.setData({
                    [`page.pageIndex`]: page,
                    goodsList
                })
            }
        })
    },
    //商品类目
    showOne() {
        const that = this;
        that.setData({
            hiddenLoading: false
        })
        myRequest.EasyHttp.post(api.reqUrls.getGoodsClass, {
            token: app.globalData.token,
        }).then(res => {
            that.setData({
                hiddenLoading: true
            })
            if (res.data.status == 1) {
                var xiaoquList = res.data.result;
                var xiaoquArr = xiaoquList.map(item => { // 此方法将校区名称区分到一个新数组中
                    return item.cat_name;
                });
                that.setData({
                    multiArray: [xiaoquArr, []],
                    xiaoquList,
                    xiaoquArr
                })

                var index = 0;
                var default_xiaoqu_id = xiaoquList[index]['cat_id']; //获取默认的校区对应的 teach_area_id
                if (default_xiaoqu_id) {
                    that.searchClassInfo(default_xiaoqu_id, index) // 如果存在调用获取对应的班级数据
                }

            } else {
                wx.showToast({
                    title: res.data.msg,
                    icon: 'none',
                })
            }
        })
    },
    searchClassInfo(xiaoqu_id, indexs) {
        var that = this;
        if (xiaoqu_id) {
            myRequest.EasyHttp.post(api.reqUrls.getgetLevel2CatByLevel1Cat, {
                token: app.globalData.token,
                cat_id: xiaoqu_id
            }).then(res => {
                that.setData({
                    hiddenLoading: true
                })
                if (res.data.status == 1) {
                    var classList = res.data.result;
                    var classArr = classList.map(item => {
                        return item.cat_name;
                    })
                    var xiaoquArr = this.data.xiaoquArr;

                    if (that.data.first) {
                        var index = 0;
                        classList.forEach(item => {
                            if (item.cat_id == that.data.goodscat_id2) {
                                index = classList.indexOf(item);
                            }
                        });
                        that.setData({
                            multiIndex: [indexs, index],
                        })
                    }

                    that.setData({
                        multiArray: [xiaoquArr, classArr],
                        classArr,
                        classList
                    })
                } else {
                    wx.showToast({
                        title: res.data.msg,
                        icon: 'none',
                    })
                }
            })

        }
    },
    bindMultiPickerColumnChange: function (e) {
        //e.detail.column 改变的数组下标列, e.detail.value 改变对应列的值
        console.log('修改的列为', e.detail.column, '，值为', e.detail.value);
        var data = {
            multiArray: this.data.multiArray,
            multiIndex: this.data.multiIndex
        };
        this.setData({
            first: false
        })
        data.multiIndex[e.detail.column] = e.detail.value;
        var teach_area_id_session = this.data.cat_id; // 保持之前的校区id 与新选择的id 做对比，如果改变则重新请求数据
        switch (e.detail.column) {
            case 0:
                var xiaoquList = this.data.xiaoquList;
                var teach_area_id = xiaoquList[e.detail.value]['cat_id'];
                if (teach_area_id_session != teach_area_id) { // 与之前保持的校区id做对比，如果不一致则重新请求并赋新值
                    this.searchClassInfo(teach_area_id);
                }
                data.multiIndex[1] = 0;
                break;
        }
        this.setData(data);
    },
    bindMultiPickerChange: function (e) {
        console.log('picker发送选择改变，携带值为', e.detail.value)
        var class_key = 0;
        var xiaoqu_key = 0;
        var xiaoquList = this.data.xiaoquList;
        var classList = this.data.classList;
        var xiaoqu_key = e.detail.value[0];
        var class_key = e.detail.value[1];
        this.setData({
            goodscat_id1: xiaoquList[xiaoqu_key]['cat_id'],
            goodscat_id2: classList.length > 0 ?  classList[class_key]['cat_id'] : '',
            multiIndex: e.detail.value
        })
    },
    /**
     * 弹出框蒙层截断touchmove事件
     */
    preventTouchMove: function () {},
    /**
     * 隐藏模态对话框
     */
    hideModal: function () {
        this.setData({
            shows: false
        });
    },
    /**
     * 对话框取消按钮点击事件
     */
    onCancel: function () {
        this.hideModal();

    },
    /**
     * 对话框确认按钮点击事件
     */
    onConfirm: function () {
        const that = this;
        this.hideModal();
        console.log(that.data.goodscat_id1, that.data.goodscat_id2, 55555)

        that.setData({
            hiddenLoading: false
        })
        myRequest.EasyHttp.post(api.reqUrls.getimport, {
            token: app.globalData.token,
            goods_id: that.data.goods_id,
            temp_cat1_id: that.data.goodscat_id1,
            temp_cat2_id: that.data.goodscat_id2,
        }).then(res => {
            that.setData({
                hiddenLoading: true
            })
            if (res.data.status == 1) {
                wx.showToast({
                    title: res.data.msg,
                    icon: 'none',
                })
                var pages = getCurrentPages(); //当前页面栈
                if (pages.length > 1) {
                    var beforePage = pages[pages.length - 2]; //获取上一个页面实例对象
                    beforePage.changeData(); //触发父页面中的方法
                }
                wx.navigateBack({
                    delta: 0,
                })
            } else if (res.data.status == -1) {
                that.setData({
                    shows: true,
                })
            } else {
                wx.showToast({
                    title: res.data.msg,
                    icon: 'none',
                })
            }
        })
    },
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})